<html><head><title>/home/joxean/proyectos/tool/dis/dbprint.py</title></head><body>
<pre>
<a name="1" /> <span style="color: #800000;">#!/usr/bin/python</span>
<a name="2" /> 
<a name="3" /> """<span style="color: #006000;"></span>
<a name="4" /> <span style="color: #006000;">Example usage of the ASM Classes library and OpenDis framework</span>
<a name="5" /> <span style="color: #006000;">A part of the Inguma Project</span>
<a name="6" /> <span style="color: #006000;"></span>"""
<a name="7" /> 
<a name="8" /> <span style="color: #804000;">import</span> sys
<a name="9" /> <span style="color: #804000;">import</span> pickle
<a name="10" /> <span style="color: #804000;">import</span> asmclasses
<a name="11" /> 
<a name="12" /> <span style="color: #804000;">def</span> <span style="color: #000080;">printProgram</span>(database):
<a name="13" />     f = file(database, "<span style="color: #006000;">r</span>")
<a name="14" />     
<a name="15" />     <span style="color: #800000;">#</span>
<a name="16" />     <span style="color: #800000;"># The database you will load will be in the following format</span>
<a name="17" />     <span style="color: #800000;">#</span>
<a name="18" />     <span style="color: #800000;"># 1) Raw data found in the .rodata section</span>
<a name="19" />     <span style="color: #800000;"># 2) The whole program in Python structures</span>
<a name="20" />     <span style="color: #800000;">#</span>
<a name="21" />     rodata, obj = pickle.load(f)
<a name="22" /> 
<a name="23" />     <span style="color: #804000;">print</span> "<span style="color: #006000;">Section .rodata: %s</span>" % hex(rodata.address)
<a name="24" />     <span style="color: #804000;">print</span> "<span style="color: #006000;">-</span>"*80
<a name="25" />     <span style="color: #804000;">print</span> repr(rodata.data)
<a name="26" />     <span style="color: #804000;">print</span> "<span style="color: #006000;">-</span>"*80
<a name="27" />     <span style="color: #804000;">print</span> 
<a name="28" /> 
<a name="29" />     showLabel = False
<a name="30" />     <span style="color: #800000;">#</span>
<a name="31" />     <span style="color: #800000;"># Iterate over all procedures in the program (List object)</span>
<a name="32" />     <span style="color: #800000;">#</span>
<a name="33" />     <span style="color: #804000;">for</span> proc <span style="color: #804000;">in</span> obj:
<a name="34" />         <span style="color: #804000;">print</span> "<span style="color: #006000;">PROCEDURE</span>", proc.name, "<span style="color: #006000;">AT 0x</span>" + proc.address
<a name="35" /> 
<a name="36" />         <span style="color: #800000;">#</span>
<a name="37" />         <span style="color: #800000;"># Iterate over all lines in the procedure</span>
<a name="38" />         <span style="color: #800000;">#</span>
<a name="39" />         <span style="color: #804000;">for</span> line <span style="color: #804000;">in</span> proc.lines:
<a name="40" />             <span style="color: #804000;">if</span> showLabel:
<a name="41" />                 <span style="color: #804000;">print</span> "<span style="color: #006000;">  %s:</span>"  % line.label
<a name="42" />             <span style="color: #804000;">if</span> line.description:
<a name="43" />                 mtype = str(type(line.description)).split("<span style="color: #006000;">'</span>")[1]
<a name="44" />                 <span style="color: #804000;">print</span> "<span style="color: #006000;">\t</span>", "<span style="color: #006000;">0x</span>" + line.address.ljust(8) + "<span style="color: #006000;">:</span>", line.code.ljust(30), "<span style="color: #006000;">;</span>", mtype.ljust(4) + "<span style="color: #006000;">:</span>", repr(line.description)
<a name="45" />             <span style="color: #804000;">else</span>:
<a name="46" />                 <span style="color: #804000;">print</span> "<span style="color: #006000;">\t</span>", "<span style="color: #006000;">0x</span>" + line.address.ljust(8) + "<span style="color: #006000;">:</span>", line.code.ljust(30)
<a name="47" /> 
<a name="48" />             <span style="color: #800000;"># Current Instruction (Assume is x86)</span>
<a name="49" />             instructions = line.code.split("<span style="color: #006000;"> </span>")
<a name="50" /> 
<a name="51" />             <span style="color: #804000;">if</span> instructions[0].find("<span style="color: #006000;">j</span>") == 0 <span style="color: #804000;">or</span> instructions[0].find("<span style="color: #006000;">call</span>") > -1:
<a name="52" />                 <span style="color: #804000;">print</span>
<a name="53" />                 showLabel = True
<a name="54" />             <span style="color: #804000;">else</span>:
<a name="55" />                 showLabel = False
<a name="56" /> 
<a name="57" />         <span style="color: #804000;">print</span> "<span style="color: #006000;">END PROCEDURE</span>"
<a name="58" />         <span style="color: #804000;">print</span>
<a name="59" /> 
<a name="60" /> <span style="color: #804000;">def</span> <span style="color: #000080;">usage</span>():
<a name="61" />     <span style="color: #804000;">print</span> "<span style="color: #006000;">Example OpenDis framework's API usage</span>"
<a name="62" />     <span style="color: #804000;">print</span>
<a name="63" />     <span style="color: #804000;">print</span> "<span style="color: #006000;">Usage:</span>", sys.argv[0], "<span style="color: #006000;">&lt;opendis format database></span>"
<a name="64" />     <span style="color: #804000;">print</span>
<a name="65" /> 
<a name="66" /> <span style="color: #804000;">def</span> <span style="color: #000080;">main</span>():
<a name="67" />     <span style="color: #804000;">if</span> len(sys.argv) == 1:
<a name="68" />         usage()
<a name="69" />         sys.exit(0)
<a name="70" />     <span style="color: #804000;">else</span>:
<a name="71" />         printProgram(sys.argv[1])
<a name="72" /> 
<a name="73" /> <span style="color: #804000;">if</span> __name__ == "<span style="color: #006000;">__main__</span>":
<a name="74" />     main()
<a name="75" /> 
<a name="76" /> </pre>
</body></html>
